<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="机器编号" prop="maccode">
        <el-input
          v-model="queryParams.maccode"
          placeholder="请输入机器编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品种编号" prop="pdfcode">
        <el-input
          v-model="queryParams.pdfcode"
          placeholder="请输入品种编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="织轴卡号" prop="shaftcode">
        <el-input
          v-model="queryParams.shaftcode"
          placeholder="请输入织轴卡号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="ordercode">
        <el-input
          v-model="queryParams.ordercode"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="操作员" prop="opworker">
        <el-select v-model="queryParams.opworker" placeholder="请选择操作员" clearable size="small">
          <el-option
            v-for="dict in opworkerOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-plus"
          size="big"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sizeList" @selection-change="handleSelectionChange">
      <el-table-column label="机器编号" align="center" prop="maccode"/>
      <el-table-column label="品种编号" align="center" prop="pdfcode"/>
      <el-table-column label="织轴卡号" width="130" align="center" prop="shaftcode"/>
      <el-table-column label="订单编号" align="center" prop="ordercode"/>
      <el-table-column label="产地" align="center" prop="productArea"/>
      <el-table-column label="批次" align="center" prop="batchNo"/>
      <el-table-column label="色号" align="center" prop="colorNo"/>
      <el-table-column label="班次日期" align="center" prop="shiftdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.shiftdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="班次类型" align="center" prop="shifttype">
        <template slot-scope="scope">
          <dict-tag :options="shifttypeOptions" :value="scope.row.shifttype"/>
        </template>
      </el-table-column>
      <el-table-column label="是否跨班次" align="center" prop="iscrossshift">
        <template slot-scope="scope">
          <dict-tag :options="iscrossshiftOptions" :value="scope.row.iscrossshift"/>
        </template>
      </el-table-column>
      <el-table-column label="上机时间" align="center" prop="sjtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sjtime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="了机时间" align="center" prop="ljtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ljtime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="轴状态" align="center" prop="beamstatus">
        <template slot-scope="scope">
          <dict-tag :options="beamstatusOptions" :value="scope.row.beamstatus"/>
        </template>
      </el-table-column>

      <el-table-column label="当前轴长(m)" align="center" prop="beamlength"/>
      <!--      <el-table-column label="当前轴长" align="center" prop="beamlength"/>-->


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-show="scope.row.beamstatus==0"
            size="big"
            type="warning"
            icon="el-icon-orange"
            @click="handleLuozhou(scope.row)"
          >落轴
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作员" align="center" prop="opworker">
        <template slot-scope="scope">
          <dict-tag :options="opworkerOptions" :value="scope.row.opworker"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改浆纱数据汇总对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="机器编号" prop="maccode">
          <el-select v-model="form.maccode" placeholder="请选择机台">
            <el-option
              v-for="item in macCodeList"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>

          <el-button @click="copyLastSizing()" type="primary" style="margin-left: 10px">复制记录</el-button>

        </el-form-item>

        <el-form-item label="订单编号" prop="ordercode">
          <el-select v-model="form.ordercode" placeholder="请输入订单编号" filterable @change="selectTrigger"
                     style="width: 100%">
            <el-option
              v-for="item in pdfList"
              :key="item.orderNo"
              :label="item.orderNo + '--' + item.materialNo + '--' + item.construction"
              :value="item.orderNo"
            >
            </el-option>
          </el-select>
          <el-input v-model="form.ordercode" placeholder="请输入订单编号"/>
        </el-form-item>
        <el-form-item label="品种规格" prop="construction">
          <el-input v-model="form.construction"/>
        </el-form-item>
        <el-form-item label="品种编号" prop="pdtcode">
          <el-input v-model="form.pdfcode" placeholder="请输入品种编号" disabled/>
        </el-form-item>
        <el-form-item label="织轴号" prop="moreinfo">
          <el-input v-model="form.shaftcode" v-show="" placeholder="请输入织轴卡号"/>
          <el-input v-model="form.moreinfo" placeholder="请输入附加信息"/>
        </el-form-item>
        <el-form-item label="缸号" prop="jarNo">
          <el-input v-model="form.jarNo" style="width: 250px" placeholder="请输入缸号" disabled/>
          <el-button @click="changeJar()" type="success" v-show="isChangeJar" style="margin-left: 10px">换缸</el-button>
        </el-form-item>
        <el-form-item label="产地" prop="productArea">
          <el-input v-model="form.productArea" placeholder="请输入产地"/>
        </el-form-item>
        <el-form-item label="批次" prop="batchNo">
          <el-input v-model="form.batchNo" placeholder="请输入批次"/>
        </el-form-item>
        <el-form-item label="色号" prop="colorNo">
          <el-input v-model="form.colorNo" placeholder="请输入色号"/>
        </el-form-item>
        <el-form-item label="操作员" prop="opworker">
          <el-select v-model="form.opworker" placeholder="请选择操作员">
            <el-option
              v-for="dict in opworkerOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import {
        listSize,
        getSize,
        getCurlength,
        selectNewestZhou,
        getInitData,
        getShiftDataAndType,
        luozhouoperation,
        delSize,
        addSize,
        updateSize,
        exportSize
    } from "@/api/mesana/size";
    import year from "../../../components/Crontab/year";

    export default {
        name: "Size",
        data() {
            return {
                // 定时器
                timer: null,
                isChangeJar: false,
                // 遮罩层
                loading: true,
                // 导出遮罩层
                exportLoading: false,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 浆纱数据汇总表格数据
                sizeList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 班次类型字典
                shifttypeOptions: [],
                // 是否跨班次字典
                iscrossshiftOptions: [],
                // 轴状态字典
                beamstatusOptions: [],
                // 操作员字典
                opworkerOptions: [],
                // 可上机机器列表
                macCodeList: [],
                construction: null,
                // 品种列表
                pdfList: [],
                // 今天织轴数量
                todayNum: 0,

                zlength: null,

                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    maccode: null,
                    pdfcode: null,
                    shaftcode: null,
                    ordercode: null,
                    shiftdate: null,
                    shifttype: null,
                    sjtime: null,
                    ljtime: null,
                    beamstatus: null,
                    opworker: null,
                    curlength: null,
                    beamlength: null,
                    setlength: null,
                    regain: null,
                    temp1: null,
                    temp2: null,
                    temp3: null,
                    temp4: null,
                    temp5: null,
                    tempa: null,
                    tempb: null,
                    pressurea: null,
                    pressureb: null,
                    pressuretuosha: null,
                    tensionza1: null,
                    tensionzb1: null,
                    tensionz4: null,
                    tensionz5: null,
                    yeweia: null,
                    yeweib: null,
                    tempwax1: null,
                    tempwax2: null,
                    stretchz2a: null,
                    stretchz3a: null,
                    stretchz2b: null,
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    maccode: [
                        {required: true, message: "机器编号不能为空", trigger: "blur"}
                    ],
                    pdfcode: [
                        {required: true, message: "品种编号不能为空", trigger: "blur"}
                    ],
                    shaftcode: [
                        {required: true, message: "织轴卡号不能为空", trigger: "blur"}
                    ],
                    ordercode: [
                        {required: true, message: "订单编号不能为空", trigger: "blur"}
                    ],
                    /*opworker: [
                        {required: true, message: "操作员不能为空", trigger: "change"}
                    ],*/
                    moreinfo: [
                        {required: true, message: "轴号不能为空", trigger: "blur"}
                    ],
                    jarNo: [
                        {required: true, message: "缸号不能为空", trigger: "blur"}
                    ],
                }

            };
        },
        created() {
            this.getList();
            this.getDicts("dict_shiftType").then(response => {
                this.shifttypeOptions = response.data;
            });
            this.getDicts("dict_isCrossShift").then(response => {
                this.iscrossshiftOptions = response.data;
            });
            this.getDicts("dict_beamstatus").then(response => {
                this.beamstatusOptions = response.data;
            });
            this.getDicts("dict_sizeworkers").then(response => {
                this.opworkerOptions = response.data;
            });
        },
        watch: {
            sizeList: {
                handler(newname, oldname) {
                },
                deep: true,
                immediate: true
            }
        },

        mounted() {
            this.getList();
            this.timer = setInterval(() => {
                setTimeout(this.getList(), 0)
            }, 1000 * 60)
        },
        beforeDestroy() {
            clearInterval(this.timer);
            this.timer = null;
        },
        methods: {
            /** 查询浆纱数据汇总列表 */
            getList() {

                this.loading = true;
                this.loading = true;
                getShiftDataAndType().then(response => {
                    this.queryParams.shiftdate = response.data.shiftdate;
                    this.queryParams.shifttype = response.data.shifttype;
                }).then(() => {
                    listSize(this.queryParams).then(response => {
                        this.sizeList = response.rows;
                        this.total = response.total;
                        this.loading = false;
                    })
                });


            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    maccode: null,
                    pdfcode: null,
                    shaftcode: null,
                    ordercode: null,
                    shiftdate: null,
                    shifttype: null,
                    moreinfo: null,
                    productArea: null,
                    batchNo: null,
                    colorNo: null,
                    construction: null,
                    iscrossshift: 0,
                    sjtime: null,
                    ljtime: null,
                    beamstatus: 0,
                    jarNo: null,
                    opworker: null,
                    station: null,
                    curlength: null,
                    beamlength: null,
                    setlength: null,
                    regain: null,
                    temp1: null,
                    temp2: null,
                    temp3: null,
                    temp4: null,
                    temp5: null,
                    tempa: null,
                    tempb: null,
                    pressurea: null,
                    pressureb: null,
                    pressuretuosha: null,
                    tensionza1: null,
                    tensionzb1: null,
                    tensionz4: null,
                    tensionz5: null,
                    yeweia: null,
                    yeweib: null,
                    tempwax1: null,
                    tempwax2: null,
                    stretchz2a: null,
                    stretchz3a: null,
                    stretchz2b: null,
                    stretchz3b: null,
                    stretchtotal: null,
                    remark: null
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加浆纱数据汇总";
                getInitData().then(response => {
                    this.macCodeList = response.data.maclist;
                    if ((response.data.maclist).length == 1) {
                        this.form.maccode = response.data.maclist[0];
                    }
                    this.pdfList = response.data.orderlist;
                    this.todayNum = response.data.todayNum;
                });


            },

            /** 落轴提交按钮*/
            handleLuozhou(row) {
                getCurlength(row.shaftcode).then(response => {
                    this.$confirm('是否对机器' + row.maccode + '上的织轴进行落轴？品种：' + row.construction +
                        '，轴号：' + row.shaftcode +
                        '，当前长度：' + response.data.curlength + '米'
                        , "提示", {
                            confirmButtonText: "确定",
                            cancelButtonText: "取消",
                            typeL: "warning",
                        }).then(function () {
                        luozhouoperation(row.id);

                    }).then(() => {
                        this.sleep(3000);
                        this.getList();

                    }).then(() => {
                        this.msgSuccess("落轴成功")
                    })
                });
            },

            /** 更换缸号*/
            changeJar() {
                var itime = new Date();
                var years = itime.getFullYear();
                var month = itime.getMonth() + 1;
                var day = itime.getDate();
                let oldJarNo = this.form.jarNo.toString();
                var oldsub = oldJarNo.substring(0,8);
                var ffsub = years.toString() + month.toString().padStart(2, "0") + day.toString().padStart(2, "0");
                if (oldsub === ffsub){
                    this.form.jarNo = this.form.jarNo + 1;
                }else{
                    this.form.jarNo = ffsub*100+1;
                }
                this.isChangeJar = false;

            },

            /** 复制上一个轴的记录*/
            copyLastSizing() {
                if (this.form.maccode != null && this.form.maccode != '') {
                    selectNewestZhou(this.form.maccode).then(response => {
                        this.form.ordercode = response.data.ordercode;
                        this.form.construction = response.data.construction;
                        this.form.pdfcode = response.data.pdfcode;
                        this.form.productArea = response.data.productArea;
                        this.form.batchNo = response.data.batchNo;
                        this.form.colorNo = response.data.colorNo;
                        this.form.opworker = response.data.opworker;
                        this.form.remark = response.data.remark;
                        this.form.jarNo = response.data.jarNo;

                        var itime = new Date();
                        var years = itime.getFullYear();
                        var month = itime.getMonth() + 1;
                        var day = itime.getDate();
                        var fsub = "J" + years.toString() + month.toString().padStart(2, "0") + day.toString().padStart(2, "0") + (this.todayNum + 1).toString().padStart(3, "0");
                        this.form.shaftcode = fsub;
                        this.isChangeJar = true;

                    });
                } else {
                    this.$alert('请您选择机台！', '温馨提醒', {
                        confirmButtonText: '确定',
                        callback: action => {
                            this.$message({
                                type: 'info',
                                message: `action: ${action}`
                            });
                        }
                    });
                }
            },

            sleep(ms) { //sleep延迟方法2
                var unixtime_ms = new Date().getTime();
                while (new Date().getTime() < unixtime_ms + ms) {
                }
            },


            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids
                getSize(id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改浆纱数据汇总";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$confirm('确认提交？', '提示', {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    typeL: "warning",
                }).then(() => {
                    this.$refs["form"].validate(valid => {
                        if (valid) {
                            if (this.form.id != null) {
                                updateSize(this.form).then(response => {
                                    this.msgSuccess("修改成功");
                                    this.open = false;
                                    this.getList();
                                });
                            } else {
                                if (this.form.moreinfo != null && this.form.moreinfo != '') {
                                    this.form.shaftcode = this.form.shaftcode + '-' + this.form.moreinfo
                                }

                                addSize(this.form).then(response => {
                                    this.msgSuccess("新增成功");
                                    this.open = false;
                                    this.getList();
                                });
                            }
                        }
                    })
                });


            },
            selectTrigger(params) {

                var itime = new Date();
                var years = itime.getFullYear();
                var month = itime.getMonth() + 1;
                var day = itime.getDate();

                this.pdfList.forEach(item => {
                    if (item.orderNo === params) {
                        this.form.pdfcode = item.materialNo;
                        this.form.construction = item.construction;
                    }
                });
                selectNewestZhou(this.form.maccode).then(response=>{
                    if(this.form.pdfcode === response.data.pdfcode){

                        this.isChangeJar = true;
                        this.form.jarNo = response.data.jarNo;
                    }else{
                        this.isChangeJar = false;
                        // new pdf
                        let oldJarNo = response.data.jarNo.toString();
                        var oldsub = oldJarNo.substring(0,8);
                        var ffsub = years.toString() + month.toString().padStart(2, "0") + day.toString().padStart(2, "0");
                        if (oldsub === ffsub){
                            this.form.jarNo = response.data.jarNo + 1;
                        }else{
                            this.form.jarNo = ffsub*100+1;
                        }

                    }

                });

                var fsub = "J" + years.toString() + month.toString().padStart(2, "0") + day.toString().padStart(2, "0") + (this.todayNum + 1).toString().padStart(3, "0");
                this.form.shaftcode = fsub;
            },

            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$confirm('是否确认删除浆纱数据汇总编号为"' + ids + '"的数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    return delSize(ids);
                }).then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                }).catch(() => {
                });
            },
            /** 导出按钮操作 */
            handleExport() {
                const queryParams = this.queryParams;
                this.$confirm('是否确认导出所有浆纱数据汇总数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(() => {
                    this.exportLoading = true;
                    return exportSize(queryParams);
                }).then(response => {
                    this.download(response.msg);
                    this.exportLoading = false;
                }).catch(() => {
                });
            }
        }
    };
</script>
